package org.example.game.history;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/07/28 11:29
 */
public class Solution4804 {

    List<Integer> xList = new ArrayList<>();
    List<Integer> yList = new ArrayList<>();

    // 如果两个点的圆点相同，保留直径大的
    // 如果圆心+半径，超出X Y， 丢弃圆点
    List<Integer> removeList = new ArrayList<>();
    HashMap<Integer, List<Integer>> lineMap = new HashMap<>();

    public static void main(String[] args) {
        System.out.println(new Solution4804().canReachCorner(5, 8, new int[][]{{4,7,1}}));
    }

    public boolean canReachCorner(int X, int Y, int[][] circles) {
        //1 是否交集X Y 轴  两个都交集的，达成，
        //2 有一个交集X的，需要通过连线找到另外一个交集Y轴
        //3 X Y有一个没有交集的， 一定无法达成
        // N个中心点
        // 每个点的交集点可以相连
        for (int i = 0; i < circles.length; i++) {
            boolean x = circles[i][2] >= circles[i][1];
            boolean y = circles[i][2] >= circles[i][0];
            if(x && y){
                return false;
            } else if (x) {
                xList.add(i);
            } else if (y) {
                yList.add(i);
            }
        }
        if(xList.isEmpty() || yList.isEmpty()){
            return true;
        }


        for (int i = 0; i < circles.length-1; i++) {
            for (int j = i+1; j < circles.length; j++) {
                if(calculateDistance1(circles[i][0], circles[i][1], circles[j][0], circles[j][1], (circles[i][2] + circles[j][2]))){
                    lineMap.putIfAbsent(i, new ArrayList<>());
                    lineMap.putIfAbsent(j, new ArrayList<>());
                    lineMap.get(i).add(j);
                    lineMap.get(j).add(i);
                }
            }
        }

        for (Integer index : xList) {
            List<Integer> linkPoint = new ArrayList<>();
            linkPoint.add(index);
            if(findY(linkPoint, index)){
                return false;
            }
        }
        return true;
    }

    private boolean findY(List<Integer> linkPoint, Integer index) {
        List<Integer> lineList = new ArrayList<>(lineMap.get(index));
        for (Integer nextIndex : lineList) {
            if(linkPoint.contains(nextIndex)){
                continue;
            }
            if(yList.contains(nextIndex)){
                return true;
            }
            linkPoint.add(nextIndex);
            return findY(linkPoint, nextIndex);
        }
        return false;
    }

    public static boolean calculateDistance1(int x1, int y1, int x2, int y2, int distance) {
        int deltaX = x2 - x1;
        int deltaY = y2 - y1;
        return Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)) <= distance;
    }

}
